package values.skill
{
	import core.manager.battle.BattleCountManager;
	import core.manager.battle.BattleEffectManager;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import values.avatar.AvatarCampTypeKey;
	import values.avatar.IAvatarValue;
	
	public class BuffDebuffEffectValue extends EventDispatcher implements IBuffDebuffEffectValue
	{
		public function BuffDebuffEffectValue()
		{
			super();
		}
		
		public function dispose():void
		{
			if(_hitter.buff == this)
			{
				_hitter.buff = null;
			}
			if(_hitter.debuff == this)
			{
				_hitter.debuff = null;
			}
			tempV = null;
			_owner = null;
			_hitter = null;
			_skillValue = null;
		}
		
		protected var _owner:IAvatarValue;
		
		protected var _hitter:IAvatarValue;
		
		protected var _skillValue:SkillValue;
		
		protected var _baseDamageNumber:Number;
		
		protected var tempV:BuffDebuffEffectTemplateValue;
		
		public function init(owner:IAvatarValue, hitter:IAvatarValue, skillValue:SkillValue, baseDamageNumber:Number=0):void
		{
			_owner = owner;
			_hitter = hitter;
			_skillValue = skillValue;
			_baseDamageNumber = baseDamageNumber;
			tempV = _skillValue.template.buffDebuff;
			
			leftRound = skillValue.getLevelValueByTag(tempV.round);
			if(tempV.target == AvatarCampTypeKey.ME)
			{
				if(owner.buff.round == leftRound)
				{
					dispose();
					return;
				}
				_hitter = owner;
			}
			if(!tempV.isBuff)
			{
				if(!skillValue.checkSkillHasLogic(LogicEffectValue.IGNORE_RESIST) && 
					BattleCountManager.countRandom(BattleCountManager.countResistRate(_owner , _hitter)))
				{
					dispose();
					return;
				}
			}
			
			
			if(tempV.isBuff)
			{
				hitter.buff = this;
			}
			else
			{
				hitter.debuff = this;
			}
			
			
			
			var num:Number;
			for each(var proV:BuffDebuffProTemplateValue in tempV.proDic)
			{
				if(!proV.needUpdate && hitter[proV.proName] != null)
				{
					if(proV.needBaseNum)
					{
						num = baseDamageNumber * skillValue.getLevelValueByTag(proV.baseNum);
						hitter[proV.proName] -= num;
					}
					else
					{
						num = SkillNumTypeKey.getResult(proV.numType , hitter[proV.proName] , skillValue.getLevelValueByTag(proV.baseNum));
						hitter[proV.proName] -= num;
					}
					
					BattleEffectManager.showEffectValue(_owner , _hitter , {num:num , proName:proV.proName});
				}
			}
		}
		
		public function effect():void
		{
			leftRound--;
			var num:Number;
			for each(var proV:BuffDebuffProTemplateValue in tempV.proDic)
			{
				if(proV.needUpdate && _hitter[proV.proName] != null)
				{
					if(proV.needBaseNum)
					{
						num = _baseDamageNumber * _skillValue.getLevelValueByTag(proV.baseNum);
						_hitter[proV.proName] -= num;
					}
					else
					{
						num = SkillNumTypeKey.getResult(proV.numType , _hitter[proV.proName] , _skillValue.getLevelValueByTag(proV.baseNum));
						_hitter[proV.proName] -= num;
					}
				}
				
				BattleEffectManager.showEffectValue(_owner , _hitter , {num:num , proName:proV.proName});
			}
			
			if(leftRound == 0)
			{
				remove();
				return;
			}
		}
		
		protected var leftRound:int;
		
		public function get round():int
		{
			return leftRound;
		}
		
		public function get icon():String
		{
			return tempV.icon;
		}
		
		public function get name():String
		{
			return tempV.name;
		}
		
		public function get desc():String
		{
			return tempV.desc;
		}
		
		public function get isBuff():Boolean
		{
			return tempV.isBuff;
		}
		
		public function get canMove():Boolean
		{
			return true;
		}
		
		public function get canUseSkill():Boolean
		{
			return true;
		}
		
		public function get candoThings():Boolean
		{
			return true;
		}
		
		public function get canUseUltimate():Boolean
		{
			return true;
		}
		
		public function remove():void
		{
			for each(var proV:BuffDebuffProTemplateValue in tempV.proDic)
			{
				if(proV.needBack && _hitter[proV.proName] != null)
				{
					if(proV.needBaseNum)
					{
						_hitter[proV.proName] += _baseDamageNumber * _skillValue.getLevelValueByTag(proV.baseNum);
					}
					else
					{
						_hitter[proV.proName] += SkillNumTypeKey.getBackResult(proV.numType , _hitter[proV.proName] , _skillValue.getLevelValueByTag(proV.baseNum));
					}
				}
			}
			
			dispose();
		}
	}
}